<template>
	<view>

		<view v-if="btnType===1" class="add-cart" @tap="add">
			<text>加入购物车</text>
		</view>
		<view v-if="btnType===2" class="add-good-edit c-flex jc-between">
			<view class="minus" @tap="minus">-</view>
			<view class="count">{{saleCount}}</view>
			<view class="add" @tap="add">+</view>
		</view>

	</view>
</template>

<script>
	import {
		debounce,toast
	} from '@/utils/utils.js'
	export default {
		name: "addCart",
		props: {
			goodItem: {
				type: Object,
				default: null
			},
			saleCount: {
				type: Number,
				default: 0
			},
			btnType: {
				type: Number,
				default: 1
			},
			refreshCount: {
				type: Boolean,
				default: false,
			}
		},
		data() {
			return {
				count: 0,

			};
		},
		watch: {
			refreshCount: {
				immediate: true,
				handler: function(val) {
					if (val) {

						this.count = this.saleCount;
					}
				}
			}
		},
		mounted() {
			console.log('saleCount:', this.saleCount)
			this.count = this.saleCount;
		},
		methods: {
			minus: debounce(function() {
					this.$API.minusCart({
						"skuId": this.goodItem.skuId
					}).then(res => {
						setTimeout(()=>{
							toast('移除成功')
						},500)
						uni.$emit('getGood', {
							action: 'updateCart'
						})
						uni.$emit('updateGoodCount')
					}).catch(() => {

					});

				},
				500),
			add: debounce(function() {

				this.addCart()

			}, 500),
			addCart() {
				console.log(this.$store.getters.userSeleAddr)
				if(!this.$store.getters.userSeleAddr){
					toast('请先添加配送地址')
					return
				}else if(this.$store.getters.userSeleAddr.auditStatus !==1){
					toast('当前配送信息还未审核通过')
					return
				}
				let data = {
					"skuId": null
				}
				if (this.btnType === 1) {
					data.skuId = this.goodItem.id
				} else if (this.btnType === 2) {
					data.skuId = this.goodItem.skuId
				}
				this.$API.addCart(data).then(res => {
					setTimeout(()=>{
						toast('添加成功')
					},500)
					uni.$emit('getGood', {
						action: 'updateCart'
					})
					uni.$emit('updateGoodCount')

				}).catch(() => {

				});
			}

		}
	}
</script>

<style lang="scss" scoped>
	.add-cart {
		width: 144rpx;
		height: 56rpx;
		background: linear-gradient(180deg, #F18858 0%, #E86547 100%);
		border-radius: 14rpx;
		text-align: center;

		text {
			font-family: PingFang-SC, PingFang-SC;
			font-weight: 500;
			font-size: 24rpx;
			color: #FFFFFF;
			line-height: 56rpx;
		}
	}

	.add-good-edit {
		width: 145rpx;
		height: 53rpx;
		border-radius: 24rpx;
		border: 2rpx solid #F0F0F0;
		text-align: center;

		view {
			width: 33.33%;
			height: 100%;
			font-family: PingFang-SC, PingFang-SC;
			font-weight: 800;
			font-size: 26rpx;
			color: #333333;
			line-height: 53rpx;
		}

		.count {
			border-left: 2rpx solid #F0F0F0;
			border-right: 2rpx solid #F0F0F0;
		}
	}
</style>